package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueAssetType;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.security.utils.SecurityUtil;
import cn.micro.system.manage.common.utils.TypeHelper;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.AssetDimTypeInfo;
import cn.micro.system.manage.entity.dto.AssetDimTypeSearch;
import cn.micro.system.manage.service.AssetDimTypeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 类型信息 前端控制器
 * </p>
 *
 * @author wr
 * @since 2024-01-05
 */
@RestController
@RequestMapping("/manage/assetDimTypeInfo")
@RequiredArgsConstructor
@Validated
public class AssetDimTypeInfoController {

    private final AssetDimTypeInfoService assetDimTypeInfoService;

    @GetMapping("/{page}/{size}")
    @PreAuthorize("hasAuthority('bnt.assetType.list')")
    public Object list(@PathVariable int page, @PathVariable int size, AssetDimTypeSearch search){
        return Result.success(new PageInfo<>(assetDimTypeInfoService.listByPage(page, size, search)));
    }

    @GetMapping("detail/{id}")
    @PreAuthorize("hasAuthority('bnt.assetType.list')")
    public Object detail(@PathVariable("id") Long id){
        return Result.success(assetDimTypeInfoService.detail(id));
    }

    @PostMapping("save")
    @Log(title = "类型管理--新增类型信息表", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.assetType.save')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniqueAssetType @RequestBody AssetDimTypeInfo assetDimTypeInfo){
        assetDimTypeInfo.setCreateBy(SecurityUtil.getUserId());
        if (assetDimTypeInfoService.save(assetDimTypeInfo)){
            return Result.success();
        }
        return Result.fail();
    }


    @PutMapping("edit")
    @Log(title = "类型管理--修改类型信息表", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.assetType.edit')")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @UniqueAssetType @RequestBody AssetDimTypeInfo assetDimTypeInfo){
        if (assetDimTypeInfoService.updateById(assetDimTypeInfo)){
            return Result.success();
        }
        return Result.fail();
    }


    @DeleteMapping("delete/{id}")
    @Log(title = "类型管理--删除类型信息表", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.assetType.remove')")
    public Object delete(@PathVariable Long id){
        if (!assetDimTypeInfoService.removeById(id)){
            return Result.fail();
        }
        return Result.success();
    }

    @GetMapping("dropTreeList")
    public Object dropTreeList(){
        List<AssetDimTypeInfo> list = assetDimTypeInfoService.list();
        return Result.success(TypeHelper.bulidDropTree(list));
    }

    /**
     * 获取主类型列表
     * @return
     */
    @GetMapping("getParentDropList")
    public Object getParentDropList(){
        List<AssetDimTypeInfo> list = assetDimTypeInfoService.list(new LambdaQueryWrapper<AssetDimTypeInfo>().eq(AssetDimTypeInfo::getParentId, 0));
        return Result.success(list);
    }

}
